package com.example.music.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.example.music.dto.PageInfoBT;
import com.example.music.dto.user.UserSelectDto;
import com.example.music.entity.User;
import com.example.music.manage.UserManage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * Created by yinhuachang on 2018/4/23
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserManage userManage;
    @RequestMapping("list")
    public String list(@ModelAttribute(value="userSelect") UserSelectDto userSelectDto, Model model){
        System.out.println("userName--->"+userSelectDto.getUserName());
        String userName = userSelectDto.getUserName();
        int indexPage=1;
        int pageSize=10;
        String index=userSelectDto.getIndexPage();
        String size=userSelectDto.getPageSize();
        if(StringUtils.isNotBlank(index)){
            indexPage=Integer.parseInt(index);
        }
        if(StringUtils.isNotBlank(size)){
            pageSize=Integer.parseInt(size);
        }
        System.out.println("indexPage->"+indexPage+":"+pageSize);
        Page page = new Page(indexPage,pageSize,"userName",true);
        PageInfoBT<User> userList= userManage.getUserList(userName,page);

        model.addAttribute("userList",userList.getContent());
        model.addAttribute("userTotle",userList.getTotal());
        model.addAttribute("indexPage",indexPage);
        model.addAttribute("pageSize",pageSize);
        System.out.println("userList-->"+userList.getContent()+":"+userList.getTotal());
        return "sys/user/userList";
    }

    @RequestMapping("select")
    public String login(Model model){
        model.addAttribute("userSelect",new UserSelectDto());
        return "sys/user/userSelect";
    }

    @RequestMapping("edit")
    public String edit(@RequestParam String id,@RequestParam String flag,Model model){
        System.out.println("id--->"+id+"  flag-->"+flag);
        model.addAttribute("flag",flag);
        if("add".equals(flag)){
            model.addAttribute("user",new User());
            return "sys/user/userEdit";
        }
        User user =userManage.queryUserInfo(id);
        System.out.println("user-->"+user);
        model.addAttribute("user",user);
        return "sys/user/userEdit";
    }

    @RequestMapping("saveEdit")
    public String saveEdit(@ModelAttribute(value="user") User user,@RequestParam String flag,Model model){
        System.out.println(flag+"-《user"+user);
        boolean rel = userManage.updateEdit(user,flag);
        if(!rel){
            return "fail";
        }
        return "success";
    }

    @RequestMapping(value = "deleteUser")
    @ResponseBody
    public String deleteUser(@RequestParam String id){
        System.out.println("id-->"+id);
        boolean rel = userManage.deleteEdit(Long.parseLong(id));
        if(!rel){
            return "fail";
        }
        return "ok";
    }

}
